import { Injectable } from '@nestjs/common'
import { UsersService } from '../users/users.service'
import { JwtService } from '@nestjs/jwt'

@Injectable()
export class AuthService {
  constructor(
    private usersService: UsersService,
    private jwtService: JwtService
  ) {}

  async signIn(data: any): Promise<{ token: string }> {
    const payload = await this.usersService.login(data)

    return {
      token: await this.jwtService.signAsync(payload),
      ...payload
    }
  }

  async register(data: any): Promise<{ token: string }> {
    const payload = await this.usersService.register(data)

    return {
      token: await this.jwtService.signAsync(payload),
      ...payload
    }
  }
}
